 /**
This file is part of Studentport.

    Studentport is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Studentport is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

**/
package com.groupalpha.studentport;

import java.util.List;

import org.apache.http.cookie.Cookie;

import com.httpclasses.perfectConnect;
import android.webkit.DownloadListener;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

/**
 * 
 * will launch a webbrowser loaded with the selected news url within app.
 * 
 * @author GroupAlpha
 * 
 */
public class NewsBrowser extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		final Activity MyActivity = this;
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newsbrowser);
		final Button logoffButton = (Button) findViewById(R.id.button2);
		final WebView web = (WebView) findViewById(R.id.newsBrowser);
		web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
		web.getSettings().setBuiltInZoomControls(true);
		web.getSettings().setSupportZoom(true);
		Bundle extras = getIntent().getExtras();

		String myUrl = "http://student.portal.chalmers.se";
		CookieSyncManager.createInstance(this);

		CookieManager cookieManager = CookieManager.getInstance();
		// List<Cookie> cookies = null;

		List<Cookie> cookies = perfectConnect.getInstance().getCookies();
		if (cookies == null || cookies.isEmpty()) {
			Intent intent = new Intent(NewsBrowser.this,
					StudentportActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other
																// Activities
																// from stack
			startActivity(intent);
		} else {

			for (Cookie x : cookies) {
				Cookie sessionCookie = x;
				if (sessionCookie != null) {
					String cookieString = sessionCookie.getName() + "="
							+ sessionCookie.getValue() + "; domain="
							+ sessionCookie.getDomain();
					cookieManager.setCookie(myUrl, cookieString);
					CookieSyncManager.getInstance().sync();
				}
			}

			if (extras != null) {

				web.setWebChromeClient(new WebChromeClient() {
					public void onProgressChanged(WebView view, int progress) {
						// Make the bar disappear after URL is loaded, and
						// changes string to Loading...
						MyActivity.setTitle("Loading...");
						MyActivity.setProgress(progress * 100); // Make the bar
																// disappear
																// after URL is
																// loaded

						// Return the app name after finish loading
						if (progress == 100)
							MyActivity.setTitle("StudentportalenBrowser");
					}
				});
				web.setDownloadListener(new DownloadListener() {
					/*
					 * (non-Javadoc)
					 * 
					 * @see
					 * android.webkit.DownloadListener#onDownloadStart(java.
					 * lang.String, java.lang.String, java.lang.String,
					 * java.lang.String, long)
					 */
					public void onDownloadStart(String url, String userAgent,
							String contentDisposition, String mimetype,
							long contentLength) {
						Intent intent = new Intent(Intent.ACTION_VIEW);
						intent.setType("application/pdf");
						intent.setData(Uri.parse(url));
						startActivity(intent);
					}
				});
				web.setWebViewClient(new WebViewClient() {
					@Override
					public boolean shouldOverrideUrlLoading(WebView view,
							String url) {
						view.loadUrl(url);
						return true;
					}
				});
				web.getSettings().setJavaScriptEnabled(true);
				String value = extras.getString("adress");
				web.loadUrl(value);
			}

			logoffButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent(NewsBrowser.this,
							StudentportActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes
																		// other
																		// Activities
																		// from
																		// stack
					startActivity(intent);
				}
			});

		}
	}
}